package com.tarena.lbs.business.servcie.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.servcie.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessRepository businessRepository;
    @Override
    public PageResult<BusinessVO> pageList(BusinessQuery query) {
        log.info("查询商家列表{}", query);
        PageResult<BusinessVO> pageResult=new PageResult<>( query.getPageSize(), query.getPageNo());
        PageInfo<BusinessPO> pageInfo=getPageBusiness( query);
        pageResult.setTotal(pageInfo.getTotal());
        List<BusinessPO> pos=pageInfo.getList();
        List<BusinessVO> vos=new ArrayList<>();
        if (pos!=null && pos.size()>0){
            vos=pos.stream().map(po->{
                BusinessVO vo=new BusinessVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setObjects(vos);
        return null;
    }

    @Override
    public List<BusinessVO> selectList(BusinessQuery query) {
        List<BusinessVO> business=businessRepository.selectList(query);
        return business;
    }

    @Override
    public void save(BusinessParam param) {
        System.out.println("新增商家{}"+param);
        BusinessPO po=new BusinessPO();
        po.setBusinessName(param.getBusinessName());
        po.setIndustryType(param.getIndustryType());
        po.setBusinessHeadName(param.getBusinessHeadName());
        po.setBusinessHeadPhone(param.getBusinessHeadPhone());
        po.setCompanyAddress(param.getCompanyAddress());
        po.setBusinessLicense(param.getBusinessLicense());
        po.setBusinessIntroduction(param.getBusinessIntroduction());
        po.setEntryTime(new Date());
        po.setBusinessStatus(1);
        po.setAuditRemarks(param.getAuditRemarks());
        po.setBusinessLogo(param.getBusinessLogo());
        System.out.println("加了几行");
        businessRepository.insert(po);
    }

    @Override
    public BusinessStoreVO businessStoreDetail(Integer businessId) {
        log.info("查询商家详情{}", businessId);
        BusinessPO po=businessRepository.selectById(businessId);
        if(po!=null){
            BusinessStoreVO vo=new BusinessStoreVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }
        return null;
    }

    private PageInfo<BusinessPO> getPageBusiness(BusinessQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        log.info("查询商家列表页数{}，{}", query.getPageNo(), query.getPageSize());
        log.info("查询商家列表参数{}", query);
        List<BusinessPO> pos=businessRepository.list(query);
        return new PageInfo<>(pos);
    }
}

